//
//  MVVM_BaseVCModel.swift
//  MVVM_BaseViewController
//
//  Created by alyadan on 2016/10/13.
//  Copyright © 2016年 alyadan. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

open class MVVM_BaseVCModel: MVVM_BaseViewModel {

   public var viewControllerTitle = Variable<String?>(nil)
    
    public override init(parentViewModel: MVVM_BaseViewModel? = nil) {
        super.init(parentViewModel: parentViewModel)
    }
    
    public func creatViewController(vcIndex:Int = 0) -> MVVM_BaseViewController? {
        let vc = viewController(vcIndex: vcIndex)
        
        vc?.vcModel.value = self
        vc?.hidesBottomBarWhenPushed = true
        
        return vc
    }
    
    
    open func viewController(vcIndex:Int = 0) -> MVVM_BaseViewController? {
        return MVVM_BaseViewController()
    }
    
    public func closeSelf(animated: Bool) {
        
        guard let vcCount = MVVM_navigationController?.viewControllers.count else{
            MVVM_modelController?.dismiss(animated: animated, completion: nil)
            return
        }
        
        if vcCount == 1 && MVVM_modelController?.presentingViewController != nil{ // modal mode
            MVVM_modelController?.dismiss(animated: animated, completion: nil)
        }else if vcCount > 1{
//            if var vcs = MVVM_navigationController?.viewControllers{
//               vcs = vcs.filter({ (x: UIViewController) -> Bool in
//                    x !== MVVM_modelController!
//               })
//                
//                MVVM_navigationController?.setViewControllers(vcs, animated: animated)
//            }
            let _ = MVVM_navigationController?.popViewController(animated: animated)
            
        }else{ //do nothing
            
        }
    }
    
    //MARK: interactive
    open func onTapNavLeftBtn() {
       closeSelf(animated: true)
    }
    
    
    open func onTapNavRightBtn() {
        
    }
}
